%%%%% --------------------------------------------------------------------------------
%%
%%%%***************************** Version Information ********************************
%%
%% this is the "commons.sty" package used for configure document styles
%% Copyright (C) Huangrui Mo <huangrui.mo@gmail.com>
%% This is free software: you can redistribute it and/or modify it
%% under the terms of the GNU General Public License as published by
%% the Free Software Foundation, either version 3 of the License, or
%% (at your option) any later version.
%%

%%%%%---------------------------------------------------------------------------------
%% This template has been modified from the original file provided by Huangrui Mo. 
%% Thanks to him for the original file.
%% Copyright (C) Hongyi Li <hyappstolz@outlook.com>
%% Time: January 7th, 2025.
%%%%%---------------------------------------------------------------------------------


\NeedsTeXFormat{LaTeX2e}[1995/12/01]
\ProvidesPackage{Style/commons}
[2014/10/01 v0.1 commons
macros package]
%%%%% --------------------------------------------------------------------------------
%%
%%%%******************************* Declare Options **********************************
%%
\newif\ifCOMMONS@myhdr \COMMONS@myhdrfalse
\DeclareOption{myhdr}{% user defined header and footer style
    \COMMONS@myhdrtrue%
}
\newif\ifCOMMONS@lscape \COMMONS@lscapefalse
\DeclareOption{lscape}{% landscape layout
    \COMMONS@lscapetrue%
}
\newif\ifCOMMONS@numbered \COMMONS@numberedtrue
\newif\ifCOMMONS@authoryear \COMMONS@authoryearfalse
\newif\ifCOMMONS@alpha \COMMONS@alphafalse
\DeclareOption{numbered}{% enable numbered citation style
    \COMMONS@numberedtrue%
    \COMMONS@authoryearfalse
    \COMMONS@alphafalse
}
\DeclareOption{authoryear}{% enable author year citation style
    \COMMONS@numberedfalse%
    \COMMONS@authoryeartrue
    \COMMONS@alphafalse
}
\DeclareOption{alpha}{% enable alpha citation style
    \COMMONS@numberedfalse%
    \COMMONS@authoryearfalse
    \COMMONS@alphatrue
}
\newif\ifCOMMONS@list \COMMONS@listfalse
\DeclareOption{list}{% enable enhanced list structures
    \COMMONS@listtrue%
}
\newif\ifCOMMONS@color \COMMONS@colorfalse
\DeclareOption{color}{% enable color package to use color
    \COMMONS@colortrue%
}
\newif\ifCOMMONS@background \COMMONS@backgroundfalse
\DeclareOption{background}{% enable page background
    \COMMONS@colortrue% need a color package
    \COMMONS@backgroundtrue%
}
\newif\ifCOMMONS@tikz \COMMONS@tikzfalse
\DeclareOption{tikz}{% enable tikz package for complex diagrams
    \COMMONS@colortrue% need xcolor package
    \COMMONS@tikztrue%
}
\newif\ifCOMMONS@table \COMMONS@tablefalse
\DeclareOption{table}{% enable a table package for complex tables
    \COMMONS@tabletrue%
}
\newif\ifCOMMONS@math \COMMONS@mathfalse
\DeclareOption{math}{% enable math package
    \COMMONS@mathtrue%
}
%% end-user parameters, each option is declared by one declare option command
\DeclareOption*{% handle non-implemented options
    \PackageWarning{commons}{Unknown option '\CurrentOption'}%
}
%% terminates all options processing
\ProcessOptions\relax
%%%%% --------------------------------------------------------------------------------
%%
%%%%******************************* Required Packages ********************************
%%
%% Language Setting
% \RequirePackage{fontspec}% fontspec package.
% \defaultfontfeatures{Ligatures=TeX}% use tex style to support LaTeX quoting style.
% \setmainfont{Times New Roman}
% \setCJKmainfont{SimSun}[AutoFakeBold, AutoFakeSlant]
% \newCJKfontfamily\songbf{SimSun}[AutoFakeBold]

\RequirePackage{titlesec}
\titleformat{\subsection}{\bfseries \zihao{-4} \songbf}{\thesubsection}{1em}{}

%%
%% >>> graphic packages
%%
\RequirePackage{graphicx}% graphics packages for including graphics
%\RequirePackage{caption}% caption options like:[margin=10pt,font=small,labelfont=bf]
\RequirePackage{subcaption}% subcaption package for subfigures
\RequirePackage[section]{placeins}% to prevent floats from being moved over section
%%
%% >>> Page Layout and Line Spacing
%%
\ifCOMMONS@myhdr% header and footer style
    \RequirePackage{fancyhdr}% fancy headers and footers
\fi
\ifCOMMONS@lscape% landscape layout
    \RequirePackage{fancyhdr}% fancy headers and footers
    %% Usage: \begin{landscape} [\thispagestyle{lscape}] text... \end{landscape}
    \RequirePackage{pdflscape}% landscape environment
    \RequirePackage[absolute]{textpos}% rotated page number
\fi

\RequirePackage{microtype}% improves general appearance of the text
%%
%%% >>> Natural Sciences Citations and References
%%
%% if use bibtex citation, not need to use any packages!
%% if use natbib package, then:
\ifCOMMONS@numbered% enable numbered citation style
    \RequirePackage[square,comma,numbers,sort&compress]{natbib}% square bracket
    %\RequirePackage[square,comma,super,sort&compress]{natbib}% superscripted square bracket
    %\bibliographystyle{elsarticle-num-names}% numbered scheme
    \bibliographystyle{Biblio/gbt-7714-2015-numerical}% numbered scheme
\fi
\ifCOMMONS@authoryear% enable author year citation style
    \RequirePackage{natbib}% author year citation mode is the default
    %\bibliographystyle{elsarticle-harv}% author year scheme
    \bibliographystyle{Biblio/gbt-7714-2015-author-year}% numbered scheme
\fi
\ifCOMMONS@alpha% enable alpha citation style
    \RequirePackage[square,comma,numbers]{natbib}% square bracket
    \bibliographystyle{alpha}% alpha scheme
\fi
%%
%% >>> List Structures
%%
\ifCOMMONS@list% enable enhanced list and verbatim structures
    \RequirePackage{enumitem}% configure the enumerate environment
    \RequirePackage{listings}% useful for Algorithm and Coding
    %% Improves the verbatim environment, fixing some bugs. Moreover
    %% it provides the comment environment, that lets you add multiple-line
    %% comments or easily comment out big parts of the code.
    \RequirePackage{verbatim}
\fi
%%
%%% >>> Color
%%
\ifCOMMONS@color% enable color package to use color
    %\RequirePackage{color}%
    \RequirePackage[usenames,dvipsnames,table]{xcolor}%
\fi
%%
%%% >>> Tables
%%
\ifCOMMONS@table% enable a table for complex tables
    %% ctable package imports the array, tabularx and booktabs packages
    %% all commands from those packages are available as well.
    \RequirePackage{ctable}%
\fi
%%
%-> List structures
%-
\ifCOMMONS@list% enable enhanced list and verbatim structures
    \RequirePackage{verbatim}% improve verbatim environment
    \RequirePackage{enumitem}% configure the enumerate environment
    \RequirePackage{listings}% source code
    \RequirePackage[linesnumbered, ruled]{algorithm2e}% algorithm
    %\RequirePackage[ruled]{algorithm2e}% algorithm
    \SetKwRepeat{Do}{do}{while}
    \renewcommand{\listalgorithmcfname}{List of Algorithms}
    \renewcommand{\algorithmcfname}{Algorithm}
    \renewcommand{\algorithmcfname}{算法}
\fi
%-
%% >>> Math packages
%%
\ifCOMMONS@math% enable some extra math package
    \RequirePackage{mathtools,amstext,amsfonts,amssymb}
\fi
%%
%% >>> Tkiz
%%
%% Draw graphics directly with TeX commands
%% LaTeX/PGF/TikZ to draw procedural graphics directly with TeX commands.
%% Using TikZ in a LaTeX document requires loading the tikz package:
\ifCOMMONS@tikz% enable tikz for complex diagrams
    \RequirePackage{tikz}%
    %% This will automatically load the pgf package. To load further libraries use:
    \usetikzlibrary{
        positioning,
        arrows,
        calc,
        trees
    }%
\fi
%% Examples for libraries are "arrows", "automata", "backgrounds", "calendar",
%% "chains", "matrix", "mindmap", "patterns", "petri", "shadows", "shapes.geometric",
%% "shapes.misc", "spy", "trees".
%%
%% >>> hyperref packages
\RequirePackage[xetex]{hyperref}

%% Setting Hyperlinks
\hypersetup{% set hyperlinks.
    unicode=true,% allows non-Latin based languages in bookmarks
    %pdftitle={},% title
    %pdfauthor={},% author
    %pdfsubject={},% subject
    %pdfkeywords={keyword1}{key2} {key3},% list of keywords, and uncomment this line if desired
    %pdftoolbar=true,% show Acrobat's toolbar?
    %pdfmenubar=true,% show Acrobat's menu?
    pdffitwindow=false,% window fit to page when opened
    pdfstartview={FitH},% fits the width of the page to the window
    %pdfnewwindow=true,% links in new window
    %colorlinks=true,% false: boxed links; true: colored links
    %linkcolor=blue,% color of internal links
    %citecolor=green,% color of links to bibliography
    %filecolor=magenta,% color of file links
    %urlcolor=cyan,% color of external links
    bookmarksnumbered=true,% put section numbers in bookmarks
    hidelinks% remove link color and border.
}
%% it gives LaTeX the possibility to manage links
%% within the document or to any URL when you compile in PDF.
%% Just load the package in the preamble, at the end of all the
%% other packages but prior to other settings.
%%%%% --------------------------------------------------------------------------------
%%
%%%%**************************** Configuration Command *******************************
%%
%% >>> Extensions and Directories for graphics
%%
%% Declare graphic extensions for automatic selection when including graphics.
%% By avoiding supplying graphic extensions in \includegraphics command,
%% the source file can be more general and adaptive.
\ifxetex % running xelatex in PDF mode
    \DeclareGraphicsExtensions{.pdf,.eps,.ps,.tif,.jpg,.png,.bmp,.gif}%
\else
    \ifpdf % running pdfTeX in PDF mode
        \DeclareGraphicsExtensions{.pdf,.jpg,.jpeg,.png}%
        %% To support .eps, .tif for pdfTex, recommend using epstopdf converter
        %% on the command line only, rather than embedding into source files.
    \fi
\fi
\graphicspath{{Img/}}% search path for figures
%%
%%% >>> Page background:
%%
\ifCOMMONS@background% enable page background
    \definecolor{backgroundcolor}{rgb}{0.85,0.85,0.85}
    \pagecolor{backgroundcolor} % page background color, require a color package.
\fi
%%
%%% >>> Layout and Margin
%%
\RequirePackage{geometry}
\geometry{
    left=2.5cm, 
    right=2.5cm,
    bottom=0.9cm, 
    top=5.2cm, 
    headsep=23.5pt, 
    footskip=1cm, 
    bindingoffset=0.5cm
}

%\linespread{1.5}% 1.5 for "one and a half" line spacing, and 2.0 for "double" line spacing
%\setlength{\parskip}{0.5ex plus 0.25ex minus 0.25ex}% skip space a paragraph

\renewcommand{\headrule}{
  \makebox[0pt][l]{\rule[0.6\baselineskip]{\headwidth}{0.5pt}}%
  \rule[0.7\baselineskip]{\headwidth}{1.5pt}}

%%
%%% >>> Page header and footer Style
%%
%% Page styles in Latex refers to headers and footers of a document.
%% These headers/footers typically contain document titles, chapter
%% or section numbers/names, and page numbers.
%% configure fancy style
\ifCOMMONS@myhdr% user defined header and footer style
\pagestyle{fancy}%
    \providecommand{\chaptermark}{}% compatibility for non-book classes
    \providecommand{\thechapter}{}% compatibility for non-book classes
    \providecommand{\CTEXthechapter}{\thechapter.}% compatibility for non ctex classes
    %- reset style of chapter and section mark to actual name
    \renewcommand{\chaptermark}[1]{\markboth{\MakeUppercase{#1}}{}}%
    \renewcommand{\sectionmark}[1]{\markright{\MakeUppercase{#1}}{}}%
    %- deactivate uppercase effect
    \renewcommand{\MakeUppercase}[1]{#1}%
    %- Define different kinds of header and footer for different parts
    \fancypagestyle{frontmatterstyle}{% style for frontmatter前言
        \fancyhf{}% clear fields
        \fancyhead[CE]{\footnotesize \zihao{-4}\@title}% structure elements
        \fancyhead[CO]{\footnotesize \zihao{-4}\leftmark}% structure elements
        \fancyfoot[LE]{\footnotesize \zihao{5}\thepage}% page number
        \fancyfoot[RO]{\footnotesize \zihao{5}\thepage}% page number
        \renewcommand{\headrulewidth}{0.8pt}% header rule
        \renewcommand{\footrulewidth}{0pt}% footer rule
    }
    \fancypagestyle{mainmatterstyle}{% style for mainmatter主要内容
        \fancyhf{}% clear fields
        \fancyhead[CE]{\footnotesize \zihao{-4}\@title}% structure elements
        \fancyhead[CO]{\footnotesize \zihao{-4}第 \thechapter 章 \leftmark}% structure elements
        \fancyfoot[LE]{\footnotesize \zihao{5}\thepage}% page number
        \fancyfoot[RO]{\footnotesize \zihao{5}\thepage}% page number
        \renewcommand{\headrulewidth}{0.8pt}% header rule
        \renewcommand{\footrulewidth}{0pt}% footer rule
    }
    \fancypagestyle{backmatterstyle}{% header and footer style for backmatter附录
        \fancyhf{}% clear fields
        \fancyhead[CE]{\footnotesize \zihao{-4}\@title}% structure elements
        \fancyhead[CO]{\footnotesize \zihao{-4}\leftmark}% structure elements
        \fancyfoot[LE]{\footnotesize \zihao{5}\thepage}% page number
        \fancyfoot[RO]{\footnotesize \zihao{5}\thepage}% page number
        \renewcommand{\headrulewidth}{0.8pt}% header rule
        \renewcommand{\footrulewidth}{0pt}% footer rule
    }
    %- Redefine \frontmatter to include the change
    \providecommand{\frontmatter}{}% compatibility for non-book classes
    \let\myfrontmatter\frontmatter%
    \renewcommand{\frontmatter}{%
        \myfrontmatter%
        \pagestyle{frontmatterstyle}%
    }
    %- Redefine \mainmatter to include the change
    \providecommand{\mainmatter}{}% compatibility for non-book classes
    \let\mymainmatter\mainmatter%
    \renewcommand{\mainmatter}{%
        \mymainmatter%
        \pagestyle{mainmatterstyle}%
    }
    %- Redefine \backmatter to include the change
    \providecommand{\backmatter}{}% compatibility for non-book classes
    \let\mybackmatter\backmatter%
    \renewcommand{\backmatter}{%
        \mybackmatter%
        \pagestyle{backmatterstyle}%
    }
    %- Some Latex commands, like \chapter, use the \thispagestyle command
    %- to automatically switch to the plain page style, thus ignoring the
    %- page style currently in effect. To customize such pages you must
    %- redefine the plain pagestyle. If you want the plain style inherits
    %- the current style, comment all the lines in plain style definition.
    %一般默认情况下每一章的第一页不显示页眉页脚
    \fancypagestyle{plain}{%
        %\fancyhf{}% clear fields
        %\renewcommand{\headrulewidth}{0pt}% header rule
        %\renewcommand{\footrulewidth}{0pt}% footer rule
    }
\fi
%-
%-> Configure landscape environment
%-
\ifCOMMONS@lscape%
\fancypagestyle{lscape}{% landscape layout style
    \fancyhf{}% clear fields
    \fancyfoot[CE,CO]{%
    %- textpos: \begin{textblock}{<blockwidth>}[0.5,0.5](<hpos>,<vpos>) text... \end{textblock}
    %- origin of the absolute coordinate is the top-left corner of the page
    %- [0.5,0.5] means reference point of the block is the middle
    \begin{textblock}{0.1}[0.5,0.5](0.85,0.5){\rotatebox{90}{\footnotesize \thepage}}\end{textblock}% position the page number
    }
    %- set units of <blockwidth>, <hpos>, and <vpos> arguments by corresponding modules
    \setlength{\TPHorizModule}{8.5in}% set to the width of page
    \setlength{\TPVertModule}{11in}% set to the height of page
    \renewcommand{\headrulewidth}{0pt}% header rule
    \renewcommand{\footrulewidth}{0pt}% footer rule
}
\fi
\ifCOMMONS@list% enable enhanced list
    \definecolor{mygreen}{rgb}{0,0.6,0}
    \definecolor{mygray}{rgb}{0.5,0.5,0.5}
    \definecolor{mymauve}{rgb}{0.58,0,0.82}
    \lstset{%
        numberbychapter=false,% numbered sequentially or by chapter
        backgroundcolor=\color{white},% background color;
        basicstyle=\scriptsize,% font size for code
        breakatwhitespace=false,% sets if automatic breaks should only happen at whitespace
        breaklines=true,% sets automatic line breaking
        captionpos=b,% caption-position to bottom
        commentstyle=\color{mygreen},% comment style
        %deletekeywords={...},% delete keywords from the given language
        frame=single,% adds a frame around the code
        keepspaces=true,% keeps spaces in text for keeping indentation of code
        keywordstyle=\color{blue},% keyword style
        %otherkeywords={*,...},% add more keywords to the set
        numbers=left,% where to put the line-numbers; possible values are (none, left, right)
        numbersep=5pt,% how far the line-numbers are from the code
        numberstyle=\tiny\color{mygray},% the style that is used for the line-numbers
        rulecolor=\color{black},% if not set, the frame-color may be changed on line-breaks
        showspaces=false,% show spaces everywhere adding particular underscores;
        showstringspaces=false,% underline spaces within strings only
        showtabs=false,% show tabs within strings adding particular underscores
        stepnumber=2,% the step between two line-numbers. If it's 1, each line will be numbered
        stringstyle=\color{mymauve},% string literal style
        tabsize=2,% sets default tabsize to 2 spaces
        title=\lstname% show the filename of files
    }
\fi
%-
%-> Page background
%-
\ifCOMMONS@background%
    \definecolor{backgroundcolor}{rgb}{0.85,0.85,0.85}%
    \pagecolor{backgroundcolor}% background color
\fi
%%%%% --------------------------------------------------------------------------------
\endinput
%% End of file
%%%%% --------------------------------------------------------------------------------
